﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace ReportBusiness
{
    public class uMESCommonBusiness
    {
        public string FormatParaStr(string strPara)
        {
            string str = "";

            str = strPara.Replace("'", "").Replace("%","").Replace("&","");

            return str;
        }

        //剔除工序名称中的件号
        public string GetSpecNameWithOutProdName(string strSpecName)
        {
            string[] array = strSpecName.Split('-');
            int intLen = array.Length;

            string str = "";
            if (intLen >= 3)
            {
                str = array[intLen - 2].ToString() + "-" + array[intLen - 1].ToString();
            }
            else
            {
                str = strSpecName;
            }

            return str;
        }

       // /剔除批次名称中的件号
        public string ReplaceProductNameForContainer(string strResource)
        {
            string[] strArray = strResource.Split('/');
            if (strArray.Length >= 2)
            {
                return strArray[1];
            }
            else
            {
                return strResource;
            }
        }

        public uMESPagingDataDTO PagingData(DataTable dt, int currentPage, int pageSize)
        {

            dt.Columns.Add("rowno", typeof(System.Int32));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dt.Rows[i]["rowno"] = i + 1;
            }
            uMESPagingDataDTO dto = new uMESPagingDataDTO();
            dto.RowCount = dt.Rows.Count.ToString();

            if (dt.Rows.Count % pageSize != 0)
            {
                dto.PageCount = (dt.Rows.Count / pageSize + 1).ToString();
            }
            else
            {
                dto.PageCount = (dt.Rows.Count / pageSize).ToString();
            }
            int min = (currentPage - 1) * pageSize;
            int max = currentPage * pageSize;

            DataRow[] drs = dt.Select("rowno>" + min + " and rowno<=" + max, "rowno");

            DataTable tmp = dt.Clone();
            foreach (DataRow dr in drs)
            {
                tmp.ImportRow(dr);
            }

            dto.DBTable = tmp;
            return dto;
        }

        public int GetMonthDays(string strMonth)
        {
            DateTime date = Convert.ToDateTime(strMonth);
            int month = date.Month;
            int year = date.Year;


            int days = 0;
            if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
            {
                days = 31;
            }
            else if (month == 4 || month == 6 || month == 9 || month == 11)
            {
                days = 30;
            }
            else
            { // 2月份，闰年29天、平年28天  
                if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
                {
                    days = 29;
                }
                else
                {
                    days = 28;
                }
            }

            return days;
        }

    }
}
